package demo1;



// 2、无头双向链表实现
public class MyLinkList {

    private class ListNode{
        private int val;
        private ListNode next;
        private ListNode prev;

        public ListNode(int val){
            this.val = val;
        }

    }

    private ListNode head;
    private ListNode last;
    private int usedSize;
        //头插法
        public void addFirst(int data) {
            ListNode cur = new ListNode(data);
            if(head == null){
                head = cur;
                last = cur;
            }else{
                cur.next = head;
                head.prev = cur;
                head = cur;
            }
            cur.prev = null;
            usedSize++;
        }


        //尾插法
        public void addLast(int data) {
            ListNode cur = new ListNode(data);
            if(head == null){
                head = cur;
                last = cur;
            }else{
                last.next = cur;
                cur.prev = last;
                last = cur;
            }
            usedSize++;
        }


        //任意位置插入,第一个数据节点为0号下标
        public boolean addIndex(int index,int data) {
            ListNode node = new ListNode(data);
            if(index < 0 || index > size()){
                throw new RuntimeException("index位置不合法" + index);
            }
            if(index == 0){
                addFirst(data);
                return true;
            }
            if(index == size()){
                addLast(data);
                return true;
            }
            ListNode cur = head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            node.next = cur;
            node.prev = cur.prev;
            cur.prev.next = node;
            cur.prev = node;
            usedSize++;
            return true;


        }


        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key) {
            ListNode cur = head;
            while (cur != null) {
                if (cur.val == key) {
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }


        //删除第一次出现关键字为key的节点
        public void remove(int key) {
            ListNode cur = head;
            // 处理头节点就是要删除的节点的情况
            if (head != null && head.val == key) {
                if (head.next == null) {
                    // 若链表只有一个节点
                    head = null;
                    last = null;
                } else {
                    // 若链表不止一个节点
                    head = head.next;
                    head.prev = null;
                }
                usedSize--;
                return;
            }
            // 遍历链表查找要删除的节点
            while (cur != null) {
                if (cur.val == key) {
                    if (cur.next == null) {
                        // 若要删除的是尾节点
                        last = cur.prev;
                        last.next = null;
                    } else {
                        // 若要删除的是中间节点
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                    usedSize--;
                    return;
                }
                cur = cur.next;
            }
        }


        //删除所有值为key的节点
        public void removeAllKey(int key) {
            ListNode cur = head;
            // 处理头节点就是要删除的节点的情况
            if (head != null && head.val == key) {
                if (head.next == null) {
                    // 若链表只有一个节点
                    head = null;
                    last = null;
                } else {
                    // 若链表不止一个节点
                    head = head.next;
                    head.prev = null;
                }
                usedSize--;
                return;
            }
            // 遍历链表查找要删除的节点
            while (cur != null) {
                if (cur.val == key) {
                    if (cur.next == null) {
                        // 若要删除的是尾节点
                        last = cur.prev;
                        last.next = null;
                    } else {
                        // 若要删除的是中间节点
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                    usedSize--;
                    return;
                }
                cur = cur.next;
            }
        }


        //得到单链表的长度
        public int size(){
            return usedSize-1;
        }


        public void display(){
            ListNode cur = head;
            while(cur != null){
                System.out.println(cur.val);
                cur = cur.next;
            }
        }


        public void clear(){

        }
    }

